package org.darkforest.common.services.impl;

import java.util.List;

import org.darkforest.common.services.CmsSpecialCommentService;
import org.darkforest.common.util.DateUtil;
import org.darkforest.dao.mapper.CmsSpecialCommentMapper;
import org.darkforest.dao.mapper.CmsSpecialCommentUpMapper;
import org.darkforest.model.CmsSpecialComment;
import org.darkforest.model.CmsSpecialCommentUp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class CmsSpecialCommentServiceImpl implements CmsSpecialCommentService {

    @Autowired
    private CmsSpecialCommentMapper specialCommentMapper;

    @Autowired
    private CmsSpecialCommentUpMapper specialCommentUpMapper;

    @Override
    public CmsSpecialComment selectByPrimaryKey(Integer commentId) {
        return specialCommentMapper.selectByPrimaryKey(commentId);
    }

    @Override
    public List<CmsSpecialComment> selectListByCondition(CmsSpecialComment record) {
        return specialCommentMapper.selectListByCondition(record);
    }

    @Override
    public int deleteByPrimaryKey(Integer commentId) {
        return specialCommentMapper.deleteByPrimaryKey(commentId);
    }

    @Override
    public int insert(CmsSpecialComment record) {
        return specialCommentMapper.insert(record);
    }

    @Override
    public int insertSelective(CmsSpecialComment record) {
        return specialCommentMapper.insertSelective(record);
    }

    @Override
    public int updateByPrimaryKeySelective(CmsSpecialComment record) {
        return specialCommentMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKeyWithBLOBs(CmsSpecialComment record) {
        return specialCommentMapper.updateByPrimaryKeyWithBLOBs(record);
    }

    @Override
    public int updateByPrimaryKey(CmsSpecialComment record) {
        return specialCommentMapper.updateByPrimaryKey(record);
    }

    @Override
    @Transactional
    public Integer addThumbUp(CmsSpecialComment record, Integer memberId) {

        // 点赞的对象是专题
        if (record.getCommentId().intValue() == 0) {
            // SpecialComment表不需要更新
        } else {
            // 点赞的对象是某一条评论
            specialCommentMapper.addThumbUp(record);
        }

        CmsSpecialCommentUp commentUp = new CmsSpecialCommentUp();
        commentUp.setCommentId(record.getCommentId());
        commentUp.setSpecialId(record.getSpecialId());
        commentUp.setUpMemberId(memberId);
        commentUp.setUpTime(DateUtil.unixTimeStampInt().intValue());

       int insetCount = specialCommentUpMapper.insert(commentUp);

        return insetCount;
    }

    @Override
    @Transactional
    public int cancelThumbUp(CmsSpecialComment record, Integer memberId) {

        // 点赞的对象是专题
        if (record.getCommentId().intValue() == 0) {
            // SpecialComment表不需要更新
        } else {
            // 点赞的对象是某一条评论
            specialCommentMapper.cancelThumbUp(record);
        }

        CmsSpecialCommentUp commentUp = new CmsSpecialCommentUp();
        commentUp.setCommentId(record.getCommentId());
        commentUp.setSpecialId(record.getSpecialId());
        commentUp.setUpMemberId(memberId);

        // order by up_time desc
        List<CmsSpecialCommentUp>  commentUpList =  specialCommentUpMapper.selectListByCondition(commentUp);
        Integer maxUpId = commentUpList.get(0).getUpId();

        int delCount = specialCommentUpMapper.deleteByPrimaryKey(maxUpId);

        return delCount;
    }
}
